﻿using System;
using System.Diagnostics;
using System.Xml.Serialization;

namespace Seagull.Model.Alerts
{
    [DebuggerDisplay("{ToString()}")]
    public abstract class Alert
    {
        protected Alert()
        {
            Time = DateTime.Now;
            // 默认类型是报警
            AlertType = AlertType.Alert;
        }

        [XmlAttribute("类型")]
        public AlertType AlertType { get; set; }

        [XmlAttribute("时间")]
        public DateTime Time { get; set; }

        [XmlAttribute("内容")]
        public virtual string AlertContent { get; set; }

        [XmlIgnore]
        public virtual string AlertDetail
        {
            get { return AlertContent; }
            set { AlertContent = value; }
        }

        public sealed override string ToString()
        {
            return string.Format("{0}{1} {2}", Prefix, Time.ToString("yyyy/MM/dd H:mm"), AlertContent);
        }

        [XmlIgnore]
        private string Prefix
        {
            get
            {
                var keyword = "GMS";
                switch(AlertType)
                {
                    case AlertType.Message:
                        return keyword + "提示";
                    case AlertType.Alert:
                        return keyword + "报警";
                    default:
                        throw new ArgumentOutOfRangeException("AlertType");
                }
            }
        }

        public GenericAlert ToGenericAlert()
        {
            return new GenericAlert
            {
                Time = Time,
                AlertType = AlertType,
                AlertContent = AlertContent,
                AlertDetail = AlertDetail,
            };
        }
    }
}
